package myapps.ttclient.activity.ppt;

import myapps.ttclient.Helper;
import myapps.ttclient.R;
import myapps.ttclient.adapter.PPTPickingPointerAdapter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;

public class PPTPickingPointerActivity extends Activity 
		implements OnClickListener, OnItemClickListener { 

	GridView gridView;
	PPTPickingPointerAdapter pointerAdapter;

	Button btnApply;
	Button btnCancel;

	Integer[] mPointerIDs;

	int mCurPointerID = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Helper.updateFullscreenStatus(this, true);
		
		//Helper.keepScreenOn(this);
		
		this.setContentView(R.layout.picking_pointer);
		
		Intent myCallerIntent = getIntent();
		Bundle myBundle = myCallerIntent.getExtras();
		mCurPointerID = myBundle.getInt("PointerId");

		mPointerIDs = new Integer[] {
				R.drawable.p00_red_finger_large_l,
				R.drawable.p01_red_finger_large_r,
				R.drawable.p02_red_finger_small_l,
				R.drawable.p03_red_finger_small_r,
				R.drawable.p04_stick_l,
				R.drawable.p05_stick_r,
				R.drawable.p06_violet_finger_large_l,
				R.drawable.p07_violet_finger_large_r,
				R.drawable.p08_violet_finger_small_l,
				R.drawable.p09_violet_finger_small_r,
				R.drawable.p10_white_finger_large_l,
				R.drawable.p11_white_finger_large_r,
				R.drawable.p12_white_finger_small_l,
				R.drawable.p13_white_finger_small_r
		};

		gridView = (GridView) findViewById(R.id.gridview);

		pointerAdapter = new PPTPickingPointerAdapter(this, mPointerIDs);
		pointerAdapter.setmPointerSelectedID(mCurPointerID);

		gridView.setAdapter(pointerAdapter);

		gridView.setOnItemClickListener(this);

		btnApply = (Button) findViewById(R.id.btnApply);
		btnApply.setOnClickListener(this);

		btnCancel = (Button) findViewById(R.id.btnCancel);
		btnCancel.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if(v == btnApply) {
			Intent resultIntent = new Intent();

			Bundle myBundle = new Bundle();
			myBundle.putInt ("PointerId", mCurPointerID);

			resultIntent.putExtras(myBundle);

			setResult(Activity.RESULT_OK, resultIntent);
			finish();
		} else if (v == btnCancel) {
			setResult(Activity.RESULT_CANCELED, null);
			finish();
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		mCurPointerID = position;
		pointerAdapter.setmPointerSelectedID(position);
		pointerAdapter.notifyDataSetChanged();		
	}
}
